package com.o2o.cleaning.month.platform.ebusiness_plat.miya

import com.alibaba.fastjson.JSON
import org.apache.spark.sql.SparkSession
import org.elasticsearch.spark._

/**
  * @author: gaoyadi
  * @Date: 2018/6/28 14:31
  * @Description:
  * @Modify By:
  */
object Miya_es {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("SparkTest")
      .master("local[*]")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("cluster.name", "O2OElastic")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")

      .getOrCreate()
    val sc = spark.sparkContext
    val sqlcontext = spark.sqlContext

    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")
    /** *
      * 需要修改的参数信息
      */
    val year = "2021"
    val month = "12"
    val platform = "miya"
    val clusterName = "hdfs://192.168.2.188:9000"
    // 618 ------路径
    //    var address_good=s"s3a://o2o-dataproces-group/zyf/${year}/${month}20/${platform}/good"
    // 正常跑 ------路径
    var address_good = s"s3a://o2o-dataproces-group/zyf/${year}/${month}/${platform}/good"

    // 618 ------活动索引
    //        val index = "miya_2021_activity/miya_2021_618"

    // 正常 es-------索引
    val index = s"${year}_${platform}/${platform}_${year}_${month}"

    val value = spark.read.orc(address_good).toJSON.rdd.map(line => {
      val lines = JSON.parseObject(line)
      val salesAmount = lines.get("salesAmount").toString.toDouble
      val Base_info = lines.get("Base_Info").toString
      val title = lines.get("title").toString
      //          val goodRatePercentage = lines.get("goodRatePercentage").toString
      lines.put("Base_Info", Base_info)
      //          lines.put("goodRatePercentage",goodRatePercentage)
      lines.put("salesAmount", salesAmount)
      lines.remove("add_to_field")
      lines.remove("shop_pre_md5")
      lines.remove("md5_id")
      lines.remove("pre_md5")
      lines.remove("goodRatePercentage")
      lines.remove("platformName_spelling")
      lines.remove("md5_shopId")
      lines.remove("serviceCommitment")
      lines.remove("sellCount_0601")
      lines.remove("sellCount_0618")

      val thirdCategoryId = lines.get("thirdCategoryId").toString
      val subCategoryId = lines.get("subCategoryId").toString

      if (thirdCategoryId == "100150103") {
        if (!title.equals("-1")) {
          val titles = lines.getString("title")
          if (titles.toUpperCase().contains("Airpods".toUpperCase()) || titles.contains("真无线")) {
            lines.put("fourthCategoryId", "10015010301")
          } else if (!titles.contains("真无线") && titles.contains("游戏")) {
            lines.put("fourthCategoryId", "10015010302")
          }
        }
      }
      //拆分牛奶和酸奶
      if (thirdCategoryId.equals("100210501")) { //牛奶
        val title = lines.get("title").toString
        if (title.contains("低温") || title.contains("鲜奶")) {
          lines.put("fourthCategoryId", "10021050102")
        } else {
          lines.put("fourthCategoryId", "10021050101")
        }
      } else if (thirdCategoryId.equals("100210502")) { //酸奶
        val title = lines.get("title").toString
        if (title.contains("低温") || title.contains("鲜奶")) {
          lines.put("fourthCategoryId", "10021050202")
        } else {
          lines.put("fourthCategoryId", "10021050201")
        }
      }

      //1、修改 液态奶（100210501）、酸奶（100210502） 中是旺旺 或旺仔 品牌的商品  全部放到 其它牛奶乳品（100210599）中
      if (thirdCategoryId.equals("100210501") || thirdCategoryId.equals("100210502")) {
        val title = lines.get("title").toString
        if (title.contains("旺旺") || title.contains("旺仔")) {
          lines.put("thirdCategoryId", "100210599")
          lines.put("fourthCategoryId", "10021059999")
        }
      }

      //2、拆分乳液面霜
      if (thirdCategoryId.equals("100190103")) {

        val title = lines.get("title").toString
        if (title.contains("乳液")) {
          lines.put("fourthCategoryId", "10019010301")
        }
        else if (title.contains("面霜")) {
          lines.put("fourthCategoryId", "10019010302")
        }
        else {
          lines.put("fourthCategoryId", "10019010399")
        }
      }

      //3、拆分月饼---100210403
      if (subCategoryId.equals("17814")) {
        val title = lines.get("title").toString
        if (title.contains("月饼")) {
          lines.put("secondCategoryId", "1002104")
          lines.put("thirdCategoryId", "100210403")
          lines.put("fourthCategoryId", "10021040399")
        }
      }

      lines.remove("priceText_collect")
      lines
    })
    //5266149
    value.saveToEs(index, Map("es.mapping.id" -> "good_id"))
    //value.take(10).foreach(println)
  }
}
